package br.com.massai.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import br.com.massai.model.entity.GroupItem;
import br.com.massai.model.entity.ProductionGroup;
import br.com.massai.model.exceptions.ErrorCore;
import br.com.massai.model.exceptions.SystemException;

@Stateless
public class GroupItemDAO extends GenericDAO<GroupItem> implements GroupItemRepository {

	private static final long serialVersionUID = 3264250771914631345L;

	public GroupItemDAO(){
		super(GroupItem.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GroupItem> getItemByProductionGroup(ProductionGroup productionGroup) throws SystemException {
		try {
			return em.createQuery("SELECT g FROM GroupItem g WHERE g.productionGroup =:productionGroup AND g.flagFixed =:flagFixed")
					.setParameter("productionGroup", productionGroup)
					.setParameter("flagFixed", false)
					.getResultList();

		}catch (SystemException e) {
			e.printStackTrace();
			throw new SystemException(ErrorCore.ERROR_LOADING_GROUPITEM, e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GroupItem> getByProductionGroupAndDescription(ProductionGroup productionGroup, String str) throws SystemException {
		List<GroupItem> resultList = new ArrayList<>();
		try {
			String query = "SELECT g FROM GroupItem g WHERE LOWER(g.serviceItem.description) LIKE LOWER(:description) AND g.productionGroup =:productionGroup";
			resultList = em.createQuery(query)
							.setParameter("description", str != null ? "%"+str+"%" : "")
							.setParameter("productionGroup", productionGroup)
							.getResultList();

		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<>(); 
		}
		return resultList;
	}
}